import {Response} from "express";

type Code = number

export const codeSuccess = 200, codeFail = 400, codeInternalError = 500

export interface Base<T> {
    code: Code;
    msg: string;
    data: T;
}

export function success(res: Response, msg: string, data: any) {
    res.status(200).json(<Base<any>>{
        code: codeSuccess,
        msg: msg,
        data: data
    });
}

export function fail(res: Response, msg: string, data: any) {
    res.status(200).json(<Base<any>>{
        code: codeFail,
        msg: msg,
        data: data
    })
}

export function internalError(res: Response, msg: string, data: any) {
    res.status(200).json(<Base<any>>{
        code: codeInternalError,
        msg: msg,
        data: data
    });
}

export function successWithData(res: Response, data: any) {
    success(res, 'success', data)
}

export function failWithMsg(res: Response, msg: string) {
    fail(res, msg, null)
}

export function internalErrorWithMsg(res: Response, msg: string) {
    internalError(res, msg, null)
}
